package sourceagile.webservices.subversion;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.jettison.json.JSONArray;

import sourceagile.server.subversionFileRepository.specification.GenerateSpecificationFile;
import sourceagile.server.subversionFileRepository.specification.SaveSpecificationFile;
import sourceagile.webservices.GetCallback;

@SuppressWarnings("serial")
public class SaveSubversionSpecificationFileServlet extends HttpServlet {

	// http://localhost:8080/LiveSourceWebServices/SaveSubversionSpecificationFileService
	// ?repositoryURL=https://tictactoe4j.googlecode.com/svn/trunk
	// &username=alline.oliveira&password=bD8Km8se2rV9
	// &projectID=tictactoe4j

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException {

		String repositoryURL = request.getParameter("repositoryURL");

		String username = request.getParameter("username");

		String password = request.getParameter("password");

		String projectID = request.getParameter("projectID");

		JSONArray fileContent = GenerateSpecificationFile.listEntries(
				repositoryURL, username, password);

		SaveSpecificationFile.save(repositoryURL, username, password, projectID
				+ "_Specification.json", fileContent.toString());

		String answer = GetCallback.getCallback(
				request.getParameter("callback"), "OK");

		response.getWriter().println(answer);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException {

		String repositoryURL = request.getParameter("repositoryURL");

		String username = request.getParameter("username");

		String password = request.getParameter("password");

		String projectID = request.getParameter("projectID");

		JSONArray fileContent = GenerateSpecificationFile.listEntries(
				repositoryURL, username, password);

		SaveSpecificationFile.save(repositoryURL, username, password, projectID
				+ "_Specification.json", fileContent.toString());

		String answer = GetCallback.getCallback(
				request.getParameter("callback"), "OK");

		response.getWriter().println(answer);
	}
}
